package com.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Scroller;

import com.ddd.wandemo.R;
import com.util.DensityUtils;

public class ScrollerLayout2 extends LinearLayout {
    private float mXDown;
    private float mXLastMove;
    private float mXMove;
    private View mView;
    private int mOutWidth;
    private boolean isOpen = false;
    private Scroller mScroller;

    public ScrollerLayout2(Context context) {
        this(context, null);
    }

    public ScrollerLayout2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollerLayout2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        mScroller = new Scroller(context);

        setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtils.dp2px(context, 100));
        View contentView = LayoutInflater.from(context).inflate(R.layout.view_scroll_test_content, null, false);
        addView(contentView, params);


        mOutWidth = DensityUtils.dp2px(context, 150);
        LinearLayout.LayoutParams params2 = new LayoutParams(mOutWidth, DensityUtils.dp2px(context, 100));
        View outView = LayoutInflater.from(context).inflate(R.layout.view_scroll_test_out, null, false);
        addView(outView, params2);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mXDown = event.getRawX();
                mXLastMove = mXDown;
                break;
            case MotionEvent.ACTION_MOVE:
                mXMove = event.getRawX();
                float diff = Math.abs(mXMove - mXDown);
                mXLastMove = mXMove;
                if (diff > mOutWidth / 5) {
                    return true;
                }
                break;
        }
        return super.onInterceptTouchEvent(event);
    }
    //


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                mXMove = event.getRawX();
                int scrolledX = (int) (mXLastMove - mXMove);
                if (getScrollX() + scrolledX > mOutWidth) {
                    scrollTo(mOutWidth, 0);
                    return true;
                }
                if (getScrollX() + scrolledX < 0) {
                    scrollTo(0, 0);
                    return true;
                }

                scrollBy(scrolledX, 0);
                mXLastMove = mXMove;
                break;
            case MotionEvent.ACTION_UP:
                if (getScrollX() >= mOutWidth / 3) {
                    mScroller.startScroll(getScrollX(), 0, mOutWidth - getScrollX(), 0);
                } else {
                    mScroller.startScroll(getScrollX(), 0, -getScrollX(), 0);
                }
                invalidate();
                break;

            default:
                break;
        }

        return super.onTouchEvent(event);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }
}
